<style lang="less" scoped>
/deep/.el-dialog--center .el-dialog__body {
    padding: 20px 10px 28px !important;
}

/deep/.el-form-item__label {
    padding-right: 0;
}

.patient-info {
    table tr th {
        font-weight: bold
    }

    table,
    table tr th,
    table tr td {
        border: 1px solid #EBEEF5;
    }

    table {
        width: 100%;
        min-height: 40px;
        line-height: 40px;
        text-align: center;
        border-collapse: collapse;
        padding: 2px;
    }
}
</style> 

<!-- <template>
  
 <el-dialog id="splitImage" title="影像拆分" :append-to-body='true' :visible.sync="splitImageShow" :before-close="SplitClose"
        width="750px" top="3%" center>
    </el-dialog> 
</template> -->
<!--   :append-to-body='true'
  会导致bug  后续研究 -->
<template>
    <el-dialog v-el-drag-dialog title="影像拆分" :visible.sync="splitImageShow" :before-close="SplitClose" width="1110px" top="3%" center>
        <div class="of">
            <div class="patient-info">
                <div class="mb10"> <strong >主检查</strong></div>
                <table>
                    <tr>
                        <th class="w80">患者姓名</th>
                        <th class="w30">性别</th>
                        <th class="w97">患者编号</th>
                        <th class="w97">检查编号</th>
                        <th class="w160">检查项目</th>
                        <th class="w56">检查类型</th>
                        <th class="w90">检查时间</th>
                        <th class="w56">拆分后序列数</th>
                    </tr>
                    <tr>
                        <td>{{ oldInfoData.other_patient_name ? oldInfoData.other_patient_name : oldInfoData.patient_name}}</td>
                        <td>{{ oldInfoData.patient_sex | sexFilters }}</td>

                        <td >{{ oldInfoData.patient_id }}</td>

                        <td>{{ oldInfoData.accession_number }}</td>
                        <td>{{ oldInfoData.examination_item_name }}</td>
                        <td>{{ oldInfoData.modalities_in_study }}</td>
                        <td>{{ oldInfoData.study_date_time }}</td>
                        <td>{{ oldImageData.filter(e => !e.isHidden).length }}</td>
                    </tr>
                </table>
            </div>
            <div class="tc mt10 mb10">
                <i class="iconfont clr_0a70b0" style="font-size: 28px">&#xe701;</i>
            </div>
            <div class="patient-info">
                <div class="mb10"> <strong >子检查</strong></div>

                <table>
                    <tr>
                        <th class="w80">患者姓名</th>
                        <th class="w30">性别</th>
                        <th class="w97">患者编号</th>
                        <th class="w97">检查编号</th>
                        <th class="w160">检查项目</th>
                        <th class="w56">检查类型</th>
                        <th class="w90">登记时间</th>
                        <th class="w56">拆分后序列数</th>
                    </tr>

                    <tr v-for="(item, index) in splitInfoData" :key="index">
                        <td>{{ item.patient_name }}</td>
                        <td>{{ item.patient_sex }}</td>
                        <td v-if="item.patient_id === oldInfoData.patient_id">{{ item.patient_id }}</td>
                        <td v-else class="clr_red strong">{{ item.patient_id }}</td>
                        <td>{{ item.accession_number }}</td>
                        <td>{{ item.examination_item_name }}</td>
                        <td>{{ item.modalities_in_study }}</td>
                        <td>{{ item.study_date_time }}</td>
                        <td>{{ index < splitSerCount.length ? splitSerCount[index] : 0 }}</td>
                    </tr>

                </table>
            </div>
            <el-alert :title="alertTitle" type="warning" center show-icon class="mt10 mb30" :closable="false">
            </el-alert>
            <div slot="footer" class="dialog-footer fr">
                <el-button @click="SplitClose" size="medium" class="mr20">取 消</el-button>
                <el-button type="primary" @click="SplitSubmit" size="medium">确 定</el-button>
            </div>
        </div>
    </el-dialog>
</template>
<script>
//查询到的RIS患者编号与存档显示的不一致，无法进行拆分
export default {
    data() {
        return {
            timeId: null,
            splitSerCount: [],
            alertTitle:'确定要拆分影像吗？',
        }
    },

    props: ['splitImageShow', 'splitImageData', 'oldImageData', 'splitInfoData', 'oldInfoData'],
    filters: {
        sexFilters(val) {
            if (val) {
                if (val === 'M') {
                    return '男'
                } else if (val === 'F') {
                    return '女'
                }else if (val === 'O') {
                    return '未知'
                }
            } else {
                return '未知'
            }
        },
       
    },
    watch: {

        splitImageData(newVal) {

            if (newVal) {
                this.splitSerCount = []
                newVal.map(e => {
                    this.splitSerCount.push(e.length)
                })

            }

        },
        splitInfoData(newVal){
            if(newVal){
                if(newVal.some(e=>e.patient_id!==this.oldInfoData.patient_id))
                {
                    this.alertTitle = '子检查与主检查患者编号不一致，确定要拆分影像吗？'
                }else{
                    this.alertTitle = '确定要拆分影像吗？'
                }
            }
        }
    },

    methods: {

        
        timestampNowTime() {
            let nowTime = new Date()
            let times = nowTime.toLocaleString('en-US', { hour12: false }).split(" ")
            let time = times[1]
            let mdy = times[0]
            mdy = mdy.split('/')
            let month = parseInt(mdy[0]);
            let day = parseInt(mdy[1]);
            let year = parseInt(mdy[2])
            return year + '-' + (month < 10 ? '0' + month : month) + '-' + (day < 10 ? '0' + day : day) + ' ' + time
        },


        SplitClose(val) {
            // console.log('获取到的数据', this.splitInfoData);

            this.$emit('SplitClose', val)
        },
        async SplitSubmit() {
            //提交影像拆分数据

            let old_series = this.oldImageData.filter(e => !e.isHidden).map(e => e.id)
            let new_studys = this.splitInfoData.map((e, index) => {
                let obj = {}
                obj.new_series = this.splitImageData[index].map(e => e.id)
                obj.patient_id = e.patient_id
                obj.accession_number = e.accession_number
                obj.modalities_in_study = e.modalities_in_study
                obj.patient_name = e.patient_name
                obj.patient_sex = e.patient_sex
                obj.patient_age = e.patient_age
                //检查项目不用传吗？
                // obj.examination_item_name = e.examination_item_name
                obj.study_date_time = this.timestampNowTime()
                return obj

            })

            // console.log('提交接口的参数为',{old_series,new_studys});
            // return

            // 开始菊花
            this.loading = this.$loading({
                lock: true,
                text: '拆分中...',
                spinner: 'el-icon-loading',
            });

            this.$pacsApi.pacsApi.imgSplit({ old_series, new_studys }, this.oldInfoData.id).then(res => {
                let { code, msg } = res
                if (code === 0) {
                    this.$message({
                        type: 'success',
                        message: '影像拆分成功'
                    })
                    this.loading.close();
                    this.SplitClose(true)

                } else {
                    this.loading.close();
                    this.$message.error(msg)
                }
            }, err => {
                this.loading.close();
            }).catch(error => {
                this.loading.close();
            })

        }
    }

}
</script>
